package pro.coman404.tools.table;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

/**
 * @author NotFound404 at 2024/7/25
 * code code always code
 */
public class DataStreamConvertTable1 {


    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setRuntimeMode(RuntimeExecutionMode.BATCH);

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        DataStream<Row> dataStream = env.fromData(Row.of("Alice",12),Row.of("Bob",10),Row.of("Alice",100));

        Table inputTable = tableEnv.fromDataStream(dataStream).as("name","score");

        tableEnv.createTemporaryView("InputTable",inputTable);

        Table tableResult = tableEnv.sqlQuery("select name,sum(score) from InputTable group by name");

        DataStream<Row> resultStream = tableEnv.toChangelogStream(tableResult);

        resultStream.print();

        env.execute();
    }
}
